

using System;
using System.Collections.Generic;
using System.Text;

namespace JTC.MiniCommerce.Store.Services.TaxService {
  public class RegionCodeTaxProvider : ITaxProvider {

    #region Constants

    public const string DEFAULT_RATE = "DefaultRate";

    #endregion

    #region Member Variables

    private decimal _defaultTaxRate;

    #endregion

    #region Constructors

    /// <summary>
    /// Initializes a new instance of the <see cref="T:RegionCodeTaxProvider"/> class.
    /// </summary>
    private RegionCodeTaxProvider() {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:RegionCodeTaxProvider"/> class.
    /// </summary>
    /// <param name="defaultTaxRate">The default tax rate.</param>
    public RegionCodeTaxProvider(string defaultTaxRate) {
      decimal taxRate = 0.00M;
      decimal.TryParse(defaultTaxRate, out taxRate);
      _defaultTaxRate = taxRate;
    }

    #endregion

    #region ITaxProvider Members

    /// <summary>
    /// Gets the tax rate.
    /// </summary>
    /// <param name="order"></param>
    public void GetTaxRate(Order order) {
      RegionCodeTaxRate regionCodeTaxRate = new RegionCodeTaxRate(RegionCodeTaxRate.Columns.RegionCode, order.ShippingAddress.PostalCode);
      foreach(OrderItem orderItem in order.OrderItemCollection) {
        if(regionCodeTaxRate.RateId > 0) {
          orderItem.ItemTax = orderItem.ExtendedPrice * regionCodeTaxRate.Rate;
        }
        else {
          orderItem.ItemTax = orderItem.ExtendedPrice * _defaultTaxRate;
        }
      }
    }

    #endregion

  }
}
